package pv1682010;

//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;

/**
 *
 * @author xkunc7
 *
 * TODO db accesses
 */
public class User implements Comparable<User> {
    private int uco;
    private String name;
    private boolean administrator;

    public User() {
    }

    public User(int uco, String name, boolean administrator) {
        setUco(uco);
        setName(name);
        setAdministrator(administrator);
    }


    public boolean setAdministrator(boolean administrator) {
        boolean same = (this.administrator == administrator);
	this.administrator = administrator;
	return same;
    }

    public void setPassword(String newPassword, String oldPassword) {
        //TODO: test hashes and insert new Pass (to db)
    }

    public boolean setName(String name) {
	boolean same = (this.name == null) ? (name == null) : (this.name.equals(name));
	this.name = name;
	return !same;
    }

    public boolean isAdministrator() {
        return administrator;
    }

    /*private String getHashPassword() throws SQLException {
        PreparedStatement getUserPassword = null;
        try {
            getUserPassword = DBConnection.getConnection().prepareStatement(
                    "SELECT * FROM " + UserManager.DB_USER_TABLE + " WHERE " + UserManager.DB_USER_UCO_COLUMN + " = ? ;");
            getUserPassword.setInt(1, getUco());
            ResultSet rs = getUserPassword.executeQuery();
            rs.next();
            return rs.getString(name);
        } finally {
            DBConnection.closeQuietly(getUserPassword);
        }
    }*/

    public String getName() {
        return name;
    }

    public int getUco() {
        return uco;
    }

    public void setUco(int uco) {
        if (uco <= 0) throw new IllegalArgumentException("uco must be positive number");

        this.uco = uco;
    }

    @Override
    public String toString() {
        return name + " with uco " + uco;
    }

    @Override
    public boolean equals(Object obj) {
	if (obj == null) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	final User other = (User) obj;
	if (this.uco != other.uco) {
	    return false;
	}
	return true;
    }

    @Override
    public int hashCode() {
	int hash = 5;
	hash = 73 * hash + this.uco;
	return hash;
    }

    public int compareTo(User o) {
	if (equals(o)) {
	    return 0;
	}
	if (uco < o.getUco()) {
	    return -1;
	} else {
	    return 1;
	}
    }



    
}
